Skip to content

Scan files of all languages in a single opengrep run TAR-155 - #40

Merged
lolgab merged 2 commits into
mainfrom
scan-all-languages-in-one-run
Nov 6, 2026
Merged

lolgab merged 2 commits into
mainfrom
scan-all-languages-in-one-run

Conversation

@lolgab

@lolgab lolgab commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Replace the per-language file buckets with a single list and run one batched opengrep scan over all files.
  • Remove the now-unused language parameter, extensionToLanguageMap and detectLanguage.
  • Pass -j to opengrep: OPENGREP_JOBS env var if set, else cgroup v2 CPU quota (rounded up), else runtime.NumCPU().

Why

The language was never passed to opengrep (-l was commented out) and each rule only applies to files of its own languages, so grouping by language only multiplied opengrep processes, each re-loading the full ruleset. Companion change in codacy-worker makes it send all languages in one task. Without -j, a single pod could burst past its CPU request.

Batching at 1000 files per invocation (ARG_MAX protection) is unchanged.

Note: files with unmapped extensions are no longer filtered out by the wrapper; every file the worker sends is scanned (opengrep still picks rules by language).

Linear: TAR-155

Test plan

  • go build ./... and go test ./internal/tool/
  • Compare issues on multi-language sample repos vs 0.0.27
  • Watch per-pod memory (TAR-146)

@codacy-production

codacy-production Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 6 complexity

Metric Results
Complexity 6

View in Codacy

AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.

Run reviewer

TIP This summary will be updated as you push new changes.

Opengrep applies each rule only to the files of the languages it supports,
and the language was never passed to it (-l was commented out), so grouping
files by language only multiplied the number of opengrep processes, each
re-loading the full ruleset. Run one batched scan over every file instead.
Pass -j to opengrep so a single pod does not burst past its CPU allocation.
@lolgab
lolgab force-pushed the scan-all-languages-in-one-run branch from bb40256 to c836c19 Compare November 6, 2026 09:59
@lolgab
lolgab marked this pull request as ready for review November 6, 2026 10:01

@codacy-production codacy-production Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

👀 Worth a closer look

  • The cgroup-based job calculation may ignore the process's actual cgroup and inherited quotas, causing excessive parallelism on some hosts.
  • The shared file list is not reset between executions, so repeated runs can rescan prior files and duplicate results.

Codacy also found:

  • No new static-analysis or duplication issues.
  • Complexity increased in internal/tool/jobs.go; tests were added.
  • Coverage data was unavailable for this PR.

Test suggestions

  • Files from multiple languages and an unmapped extension are retained in the shared analysis list.
  • Valid and invalid OPENGREP_JOBS values are handled, including cgroup quota rounding and fallback behavior.
  • File lists larger than 1000 entries are split into multiple Opengrep invocations.
  • A complete run invokes Opengrep once per batch with all languages represented and aggregates results.
  • Repeated tool executions do not retain files from a previous execution.
Prompt proposal for missing tests
Consider implementing these tests if applicable:
1. File lists larger than 1000 entries are split into multiple Opengrep invocations.
2. A complete run invokes Opengrep once per batch with all languages represented and aggregates results.
3. Repeated tool executions do not retain files from a previous execution.

Make this review better

TIP Add Codacy review.md instructions or an AGENTS.md so the reviewer knows your conventions.

TIP How was this review? Give us feedback

Comment thread internal/tool/jobs.go
if jobs, err := strconv.Atoi(os.Getenv(jobsEnvVar)); err == nil && jobs > 0 {
return jobs
}
if jobs, ok := cgroupJobs(cgroupV2CPUMax); ok {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

Resolve the process's cgroup from /proc/self/cgroup and account for effective quotas across its ancestors before selecting the job count.

TIP React 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.

func populateFilesToAnalyseFromFiles(toolExecutionFiles []string) error {
for _, file := range toolExecutionFiles {
addFileToFilesByLanguage(file)
addFileToAnalyse(file)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

Reset the shared file list before each execution, or make it local to the execution, so repeated tool runs remain isolated.

TIP React 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.

@lolgab lolgab changed the title Scan files of all languages in a single opengrep run Scan files of all languages in a single opengrep run TAR-155 Nov 6, 2026
Comment thread internal/tool/command.go
//"--error-recovery",
"--max-memory", "5000",
//"-j", strconv.Itoa(runtime.NumCPU()),
"-j", strconv.Itoa(opengrepJobs()),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have ideia why was commented? Maybe something that existed on semgrep that was not available yet?

@machadoit machadoit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds fun to me! Maybe someone else has any input regarding passing the languages explicitly?

@lolgab
lolgab merged commit 75c6aa6 into main Nov 6, 2026
7 checks passed
@lolgab
lolgab deleted the scan-all-languages-in-one-run branch November 6, 2026 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants